package com.example.myapplication8.service

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Intent
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.widget.Toast

class MyAccessibilityService : AccessibilityService() {
    
    companion object {
        private const val TAG = "MyAccessibilityService"
        private var instance: MyAccessibilityService? = null
        const val ACTION_SERVICE_CONNECTED = "com.example.myapplication8.ACTION_ACCESSIBILITY_CONNECTED"
        const val ACTION_SERVICE_DISCONNECTED = "com.example.myapplication8.ACTION_ACCESSIBILITY_DISCONNECTED"
        
        fun getInstance(): MyAccessibilityService? = instance
    }
    
    override fun onServiceConnected() {
        try {
            instance = this
            Log.i(TAG, "无障碍服务已连接")
            
            // 配置无障碍服务
            serviceInfo = AccessibilityServiceInfo().apply {
                eventTypes = AccessibilityEvent.TYPES_ALL_MASK
                feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC
                flags = AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS or
                        AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS or
                        AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS or
                        AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
                notificationTimeout = 100
            }
            
            // 发送服务已连接的广播
            sendBroadcast(Intent(ACTION_SERVICE_CONNECTED))
            Toast.makeText(this, "无障碍服务已启动", Toast.LENGTH_SHORT).show()
        } catch (e: Exception) {
            Log.e(TAG, "无障碍服务连接失败: ${e.message}")
            e.printStackTrace()
            Toast.makeText(this, "无障碍服务启动失败", Toast.LENGTH_SHORT).show()
        }
    }
    
    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        try {
            if (event == null) return
            
            // 处理无障碍事件
            when (event.eventType) {
                AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> {
                    handleWindowStateChanged(event)
                }
                AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED -> {
                    handleWindowContentChanged(event)
                }
                // 添加其他事件处理...
            }
        } catch (e: Exception) {
            Log.e(TAG, "处理无障碍事件失败: ${e.message}")
        }
    }
    
    private fun handleWindowStateChanged(event: AccessibilityEvent) {
        try {
            val packageName = event.packageName?.toString()
            val className = event.className?.toString()
            Log.d(TAG, "Window changed: $packageName - $className")
        } catch (e: Exception) {
            Log.e(TAG, "处理窗口状态变化失败: ${e.message}")
        }
    }
    
    private fun handleWindowContentChanged(event: AccessibilityEvent) {
        try {
            val source = event.source
            if (source != null) {
                // 处理窗口内容变化...
                source.recycle() // 记得回收AccessibilityNodeInfo
            }
        } catch (e: Exception) {
            Log.e(TAG, "处理窗口内容变化失败: ${e.message}")
        }
    }
    
    override fun onInterrupt() {
        Log.w(TAG, "无障碍服务被中断")
        sendBroadcast(Intent(ACTION_SERVICE_DISCONNECTED))
        instance = null
    }
    
    override fun onUnbind(intent: Intent?): Boolean {
        Log.i(TAG, "无障碍服务已解绑")
        sendBroadcast(Intent(ACTION_SERVICE_DISCONNECTED))
        instance = null
        return super.onUnbind(intent)
    }
    
    override fun onDestroy() {
        Log.i(TAG, "无障碍服务已销毁")
        sendBroadcast(Intent(ACTION_SERVICE_DISCONNECTED))
        instance = null
        super.onDestroy()
    }
} 